package com.education.wisdom.controller;

import cn.hutool.core.date.DateUtil;
import com.education.common.utils.ApiReturnObject;
import com.education.config.BaiduAipContentCensor;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.CommentsLive;
import com.education.wisdom.service.ICommentsLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 达人直播评论
 */
@RestController
@RequestMapping("/wisdom/JrCommentsLive")
public class JrCommentsLiveController {

    @Autowired
    ICommentsLiveService iCommentsLiveService;

    @Autowired
    UserFeign userFeign;

    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;

    //添加数据
    @PostMapping("/addcomments")
    public ApiReturnObject insert(@RequestBody CommentsLive commentsLive){
        if (baiduAipContentCensor.AipText(commentsLive.getPlText()) == 2) {
            return new ApiReturnObject("201", "评论内容违规！请重新编辑发布！", "");
        }
        commentsLive.setPlCreatetime(DateUtil.date());
        int i = iCommentsLiveService.insertCommentsLive(commentsLive);
        if(i > 0) return new ApiReturnObject("200", "success", "评论成功");
        return new ApiReturnObject("201", "warning", "评价失败");

    }

    //查询数据
    @GetMapping("/selectlist")
    public ApiReturnObject selectlist(String plCatalogId){
        CommentsLive commentsLive = new CommentsLive();
        commentsLive.setPlCatalogId(Integer.parseInt(plCatalogId));
        List<CommentsLive> comments = iCommentsLiveService.selectCommentsLiveList(commentsLive);
        for (CommentsLive comment : comments) {
            Date plCreatetime = comment.getPlCreatetime();
            comment.setCreatetime(DateUtil.formatDateTime(plCreatetime));
            String plCreateuser = comment.getPlCreateuser();
            Map<String, Object> stringObjectMap = userFeign.selectByUserId(Integer.parseInt(plCreateuser));
            comment.setUserIcoUrl(stringObjectMap.get("image") + "");
            comment.setUserName(stringObjectMap.get("name") + "");
        }
        return new ApiReturnObject("200", "success", comments);

    }

}
